package com.hq.fxh.No5_User.User.Fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hq.fxh.CustomerView.CircleImageView;
import com.hq.fxh.HttpApi.UserServiceImpl;
import com.hq.fxh.Intefers.NetworkSubscriber;
import com.hq.fxh.Modle.GetUserNumData;
import com.hq.fxh.Modle.MyResponseBody;
import com.hq.fxh.Modle.SelectByMemberIdRes;
import com.hq.fxh.No0_ChengQu.Activity.HomeActivity;
import com.hq.fxh.No0_ChengQu.Login.LoginActivity;
import com.hq.fxh.No0_ChengQu.Login.RegisterActivity;
import com.hq.fxh.No3_TuiGuang.MyMoBan.Activity.item.ToastUtils;
import com.hq.fxh.No5_User.Jianli.Activity.MyJianLi;
import com.hq.fxh.No5_User.User.Activity.MyFenSi;
import com.hq.fxh.No5_User.User.Activity.MyGuanZhu;
import com.hq.fxh.No5_User.User.Activity.MyPlate;
import com.hq.fxh.No5_User.User.Activity.MyShouCang;
import com.hq.fxh.No5_User.User.Activity.QianBao;
import com.hq.fxh.No5_User.User.Activity.ReXianActivity;
import com.hq.fxh.No5_User.User.Activity.SettingActivity;
import com.hq.fxh.No5_User.User.Activity.UserXinXiActivity;
import com.hq.fxh.No6UerSpace.Activity.Space_UserActivity;
import com.hq.fxh.R;
import com.hq.fxh.Utils.AppConstant;
import com.hq.fxh.Utils.GlideUtils;
import com.hq.fxh.Utils.LogUtils;
import com.hq.fxh.Utils.SharedPreferenceUtils;
import com.hq.fxh.Utils.Tool;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import rx.Subscriber;

/**
 * /**
 *
 * @author 薛
 * @ClassName
 * @Description :
 * @Created 2017/9/13
 * @Email xlwenhao1858@sina.com
 */


public class Fragment_User extends Fragment implements View.OnClickListener {
    protected View rootView;

    protected RelativeLayout layoutUserQianBao;
    protected RelativeLayout layoutUserMyTieZhi;
    protected RelativeLayout layoutUserMyShouChang;
    protected RelativeLayout layoutUserMyFenXiang;
    protected RelativeLayout layoutUserMyJianLi;
    protected RelativeLayout layoutUserHuoDong;
    protected RelativeLayout layoutUserLianXi;
    protected RelativeLayout layoutUserYaoQing;
    protected RelativeLayout layoutUserCaoGaoXiang;
    protected TextView userSetting;
    protected CircleImageView imageUserUserHeadPic;
    protected TextView tvUserName;
    protected TextView tvUserJianJie;
    protected TextView tvUserGZNum;
    protected LinearLayout layoutUserGZ;
    protected TextView tvUserFenSiNum;
    protected LinearLayout layoutUserFenSi;
    protected TextView tvUserTieZhiNum;
    protected LinearLayout layoutUserHuoZan;
    protected ImageView imageUserBg;
    protected TextView tvUserFenSiTiSi;
    protected SelectByMemberIdRes.DataBean userdata;
    protected RelativeLayout layoutUser;
    protected ImageView imageNoUserBg;
    protected Button btnNoUserRegister;
    protected Button btnNoUserLogin;
    protected RelativeLayout layoutNoUser;
    private int memberId;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_user, null);
        initView(rootView);
        memberId = SharedPreferenceUtils.getMemberId(getContext());
        //   initData();
        getMerchantDetails();
        getUserData();
        return rootView;
    }

    private void initData() {

        if (memberId == 0) {
            layoutNoUser.setVisibility(View.VISIBLE);
            layoutUser.setVisibility(View.GONE);
        } else {
            layoutNoUser.setVisibility(View.GONE);
            layoutUser.setVisibility(View.VISIBLE);
            GlideUtils.loadImageView(getContext(), AppConstant.headimageUrl, imageUserUserHeadPic);
            Tool.setImageBG(getContext(), AppConstant.headimageUrl, imageUserBg);
        }


    }

    @Override
    public void onClick(View view) {

        if (view.getId() == R.id.user_Setting) {
            startActivity(new Intent(getContext(), SettingActivity.class));

        } else if (view.getId() == R.id.image_user_UserHeadPic) {
            if (memberId == 0) {
                toLogin();
            } else {

                Intent intent = new Intent(getContext(), UserXinXiActivity.class);
                intent.putExtra("data", userdata);
                intent.putExtra("id", memberId);
                startActivity(intent);

            }


        } else if (view.getId() == R.id.layout_User_GZ) {
            if (memberId == 0) {
                toLogin();
            } else {
                startActivity(new Intent(getContext(), MyGuanZhu.class));

            }


        } else if (view.getId() == R.id.layout_User_FenSi) {
            if (memberId == 0) {
                toLogin();
            } else {
                startActivity(new Intent(getContext(), MyFenSi.class));
                clearTiShi();
            }


        } else if (view.getId() == R.id.layout_User_QianBao) {
            if (memberId == 0) {
                toLogin();
            } else {
                startActivity(new Intent(getContext(), QianBao.class));

            }

        } else if (view.getId() == R.id.layout_User_MyTieZhi || view.getId() == R.id.layout_User_HuoZan) {
            if (memberId == 0) {
                toLogin();
            } else {
                startActivity(new Intent(getContext(), MyPlate.class));

            }


        } else if (view.getId() == R.id.layout_User_MyShouChang) {
            if (memberId == 0) {
                toLogin();
            } else {
                startActivity(new Intent(getContext(), MyShouCang.class));

            }

        } else if (view.getId() == R.id.layout_User_MyFenXiang) {
            if (memberId == 0) {
                toLogin();
            } else {
                startActivity(new Intent(getContext(), MyFenXiang.class));

            }

        } else if (view.getId() == R.id.layout_User_MyJianLi) {
            if (memberId == 0) {
                toLogin();
            } else {
                startActivity(new Intent(getContext(), MyJianLi.class));

            }
            //    addFollowerUser(1, "");
        } else if (view.getId() == R.id.layout_User_HuoDong) {
            if (memberId == 0) {
                toLogin();
            } else {
                startActivity(new Intent(getContext(), MyFenXiang.class));

            }
            //   GetFollowerList(1, 1, 20);
        } else if (view.getId() == R.id.layout_User_LianXi) {
            startActivity(new Intent(getContext(), ReXianActivity.class));
            //    addBlackUser(1, "s");
        } else if (view.getId() == R.id.layout_User_YaoQing) {
            // GetBlackList(1, 20);

        } else if (view.getId() == R.id.tv_User_Name) {

            startActivity(new Intent(getContext(), Space_UserActivity.class));

        } else if (view.getId() == R.id.btn_noUser_Register) {
            startActivity(new Intent(getContext(), RegisterActivity.class));


        } else if (view.getId() == R.id.btn_noUser_Login) {
            startActivity(new Intent(getContext(), LoginActivity.class));

        }
    }

    private void clearTiShi() {
        UserServiceImpl.clearUserData(new Callback<MyResponseBody>() {
            @Override
            public void onResponse(Call<MyResponseBody> call, Response<MyResponseBody> response) {
                MyResponseBody body = response.body();
                if (body == null) {
                    return;
                } else {
                    tvUserFenSiTiSi.setVisibility(View.GONE);

                }

            }

            @Override
            public void onFailure(Call<MyResponseBody> call, Throwable t) {


            }
        });
    }

    private void toLogin() {
        if (memberId == 0) {
            Intent intent = new Intent(getContext(), LoginActivity.class);
            startActivity(intent);
        }
    }


    private void initView(View rootView) {

        layoutUserQianBao = (RelativeLayout) rootView.findViewById(R.id.layout_User_QianBao);
        layoutUserQianBao.setOnClickListener(Fragment_User.this);
        layoutUserMyTieZhi = (RelativeLayout) rootView.findViewById(R.id.layout_User_MyTieZhi);
        layoutUserMyTieZhi.setOnClickListener(Fragment_User.this);
        layoutUserMyShouChang = (RelativeLayout) rootView.findViewById(R.id.layout_User_MyShouChang);
        layoutUserMyShouChang.setOnClickListener(Fragment_User.this);
        layoutUserMyFenXiang = (RelativeLayout) rootView.findViewById(R.id.layout_User_MyFenXiang);
        layoutUserMyFenXiang.setOnClickListener(Fragment_User.this);
        layoutUserMyJianLi = (RelativeLayout) rootView.findViewById(R.id.layout_User_MyJianLi);
        layoutUserMyJianLi.setOnClickListener(Fragment_User.this);
        layoutUserHuoDong = (RelativeLayout) rootView.findViewById(R.id.layout_User_HuoDong);
        layoutUserHuoDong.setOnClickListener(Fragment_User.this);
        layoutUserLianXi = (RelativeLayout) rootView.findViewById(R.id.layout_User_LianXi);
        layoutUserLianXi.setOnClickListener(Fragment_User.this);
        layoutUserYaoQing = (RelativeLayout) rootView.findViewById(R.id.layout_User_YaoQing);
        layoutUserYaoQing.setOnClickListener(Fragment_User.this);
        userSetting = (TextView) rootView.findViewById(R.id.user_Setting);
        userSetting.setOnClickListener(Fragment_User.this);
        imageUserUserHeadPic = (CircleImageView) rootView.findViewById(R.id.image_user_UserHeadPic);
        imageUserUserHeadPic.setOnClickListener(Fragment_User.this);
        tvUserName = (TextView) rootView.findViewById(R.id.tv_User_Name);
        tvUserName.setOnClickListener(Fragment_User.this);
        tvUserJianJie = (TextView) rootView.findViewById(R.id.tv_User_JianJie);
        tvUserGZNum = (TextView) rootView.findViewById(R.id.tv_User_GZNum);
        layoutUserGZ = (LinearLayout) rootView.findViewById(R.id.layout_User_GZ);
        layoutUserGZ.setOnClickListener(Fragment_User.this);
        tvUserFenSiNum = (TextView) rootView.findViewById(R.id.tv_User_FenSiNum);
        layoutUserFenSi = (LinearLayout) rootView.findViewById(R.id.layout_User_FenSi);
        layoutUserFenSi.setOnClickListener(Fragment_User.this);
        tvUserTieZhiNum = (TextView) rootView.findViewById(R.id.tv_User_TieZhiNum);
        layoutUserHuoZan = (LinearLayout) rootView.findViewById(R.id.layout_User_HuoZan);
        layoutUserHuoZan.setOnClickListener(Fragment_User.this);
        imageUserBg = (ImageView) rootView.findViewById(R.id.image_user_bg);
        tvUserFenSiTiSi = (TextView) rootView.findViewById(R.id.tv_User_FenSi_TiSi);
        layoutUser = (RelativeLayout) rootView.findViewById(R.id.layout_User);
        imageNoUserBg = (ImageView) rootView.findViewById(R.id.image_NoUser_bg);
        btnNoUserRegister = (Button) rootView.findViewById(R.id.btn_noUser_Register);
        btnNoUserRegister.setOnClickListener(Fragment_User.this);
        btnNoUserLogin = (Button) rootView.findViewById(R.id.btn_noUser_Login);
        btnNoUserLogin.setOnClickListener(Fragment_User.this);
        layoutNoUser = (RelativeLayout) rootView.findViewById(R.id.layout_noUser);
    }


    public void getMerchantDetails() {
        if (memberId != 0) {
            UserServiceImpl.selectByMemberId(memberId, new NetworkSubscriber<SelectByMemberIdRes>() {
                @Override
                public int onSuccess(SelectByMemberIdRes data) {
                    super.onSuccess(data);
                    if (data != null && data.getCode() == 0) {
                        userdata = data.getData();
                        initData();
                        String faceUrl = data.getData().getFace();
                        if (!TextUtils.isEmpty(faceUrl)) {
                            // ImageLoader.getInstance().load(getActivity(), ivHead, faceUrl);

                            GlideUtils.loadImage(getContext(), faceUrl, imageUserUserHeadPic);
                            Tool.setImageBG(getContext(), faceUrl, imageUserBg);

                        }
                        String name = data.getData().getNickname();
                        tvUserName.setText(data.getData().getNickname());
                    } else {
                        ToastUtils.show(getActivity(), data.getMsg());
                    }
                    return 0;
                }
            });
        } else {
            initData();
        }
    }


    public void getUserData() {
        UserServiceImpl.getUserData(memberId, new Subscriber<GetUserNumData>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(GetUserNumData responseBody) {

                GetUserNumData.DataBean data = responseBody.getData();
                if (data == null) {

                } else {
                    Tool.setIntToText(tvUserFenSiNum, data.getFans_num(), "Fragment_User");
                    Tool.setIntToText(tvUserTieZhiNum, data.getPost_num(), "Fragment_User");
                    Tool.setIntToText(tvUserGZNum, data.getFollower_num(), "Fragment_User");
                    Tool.setIntTiSiToText(tvUserFenSiTiSi, data.getNewfans_num(), "Fragment_User");
                }

            }
        });
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        LogUtils.e("onAttach");
    }

    @Override
    public void onResume() {
        super.onResume();
        if (memberId == SharedPreferenceUtils.getMemberId(getContext())) {

        } else {
            memberId = SharedPreferenceUtils.getMemberId(getContext());
            getMerchantDetails();

        }

    }


    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
  /*      if (isVisibleToUser) {
            LogUtils.e("打开我的野蛮");



        } else {
        }
        LogUtils.e("打开我的野蛮");*/
    }
}
